package com.lh.unit;

import com.alibaba.druid.sql.parser.Lexer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tool.RedisAction;

import java.util.List;
import java.util.logging.Logger;

//import org.springframework.data.redis.core.SetOperations;
//import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @author 梁昊
 * @date 2019/5/3
 * @function 得到域名、白名单、黑名单
 * @editLog
 */
@Component
public class RedisOperator {
    @Autowired
    RedisAction redisAction;

    public String getSessionKeyAndOpenId(String appId
            , String code) {
        String keyName = String.format("%s:%s", appId, code);
        return redisAction.getValue(keyName);
    }

    public boolean getOriginValid(String myOrigin) {
        if (myOrigin == null || myOrigin.length() == 0) {
            return false;
        }

//        : NO:http://sy.lh.com:1024
//        : liushuangOrigin:
        Logger logger = Logger.getLogger("chapter08");
        logger.info(String.format("0:%s",myOrigin));

        String nowOrigin = myOrigin
                .replace("https://", "")
                .replace("http://", "");
        String[] split = nowOrigin.split(":");
        int length = split.length;
        if (length > 1) {
            String replaceContent = ":" + split[length - 1];
            nowOrigin = nowOrigin.replace(replaceContent, "");
//            nowOrigin = split[0];
            logger.info(String.format("1:%s",nowOrigin));
        }

//        return redisAction.getSetIndex("domainSet", nowOrigin);
        return true;
    }

    private int insertIntoRedisSet(String setName, List<String> nameList) {
        if (nameList == null) {
            return 0;
        }
        redisAction.setSet(setName, nameList);
        return nameList.size();
    }

    private List<String> getFormRedisSet(String setName) {
        return redisAction.getSet(setName);
    }

    public List<String> getDomainList() {
        return getFormRedisSet("domainSet");
    }

    public int insertDomain(List<String> domainList) {
        return insertIntoRedisSet("domainSet", domainList);
    }

    public List<String> getBlackList() {
        return getFormRedisSet("blackSet");
    }

    public int insertBlack(List<String> blackList) {
        return insertIntoRedisSet("blackSet", blackList);
    }

    public List<String> getWhiteList() {
        return getFormRedisSet("whiteSet");
    }

    public int insertWhite(List<String> whiteList) {
        return insertIntoRedisSet("whiteSet", whiteList);
    }

    /**
     * 增加或修改指定键值
     *
     * @param key   键
     * @param value 值
     */
    public void saveString(String key, String value) {
        redisAction.saveKeyAndValue(key, value);
    }

    public String getString(String key) {
        if (key != null)
            return redisAction.getValue(key);
        else
            return null;
    }

    public void delete(String key) {
        redisAction.delete(key);
    }

}
